//
//  CWCFunnyImageCell.m
//  ChuanNews
//
//  Created by dllo on 16/4/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CWCFunnyImageCell.h"
#import "BaseHeader.h"
@implementation CWCFunnyImageCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //创建标题lable;
        self.digestLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, 355, 100)];
        //创建imgeview 放图片
        self.imgView=[[UIImageView alloc]initWithFrame:CGRectMake(10 , CGRectGetMaxY(self.digestLabel.frame) + 10, 355, 300)];
        //设置文本自动换行
        self.digestLabel.numberOfLines=0;
        //设置字体大小
        self.digestLabel.font=[UIFont systemFontOfSize:17];
        
        [self.contentView addSubview:self.digestLabel];
        [self.contentView addSubview:self.imgView];
        
        [self jxl_setDayMode:^(UIView *view) {
            CWCFunnyImageCell *cell = (CWCFunnyImageCell *)view;
            cell.backgroundColor = [UIColor whiteColor];
            cell.contentView.backgroundColor = [UIColor whiteColor];
            cell.digestLabel.backgroundColor = [UIColor whiteColor];
            cell.digestLabel.textColor = [UIColor blackColor];
        } nightMode:^(UIView *view) {
            CWCFunnyImageCell *cell = (CWCFunnyImageCell *)view;
            cell.backgroundColor = [UIColor blackColor];
            cell.contentView.backgroundColor = [UIColor blackColor];
            cell.digestLabel.backgroundColor = [UIColor blackColor];
            cell.digestLabel.textColor = [UIColor whiteColor];        }];

    }
    
    return self;
}
//获取文本的高度
+(CGFloat )textLableHeight:(NSString *)text{
    //限定宽度
    CGSize  size = CGSizeMake(360, 1000);
    
    NSDictionary *dic=@{NSFontAttributeName:[UIFont systemFontOfSize:17] };
    
    //返回一个矩形区域
    CGRect rect=[text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return rect.size.height;
}





-(void)setModel:(CWCFunModel *)model{
    if (_model != model) {
        _model = model;
        self.digestLabel.text=model.digest;
        
        //修改内容的frame
        CGFloat height=[[self class] textLableHeight:self.model.digest];
        CGRect frame = self.digestLabel.frame;
        frame.size.height = height;
        self.digestLabel.frame = frame;
        
        NSString * str = model.pixel;
        //字符串截取 获取图片的高和宽
        NSRange range = NSMakeRange(0, 3);
        NSRange range1 = NSMakeRange(4, 3);
        //将图片的高和宽转化为浮点型
        NSString * widthString=[str substringWithRange:range];
        CGFloat imgWidth=[widthString floatValue];
        
        NSString * heightString=[str substringWithRange:range1];
        CGFloat imgHeight =[heightString floatValue];
        //获得imgview的高度
        CGFloat  heightX= 355 * imgHeight / imgWidth;
        //修改imgview的frame
        CGRect imgFrame = self.imgView.frame;
        imgFrame.size.height = heightX;
        imgFrame.origin.y = CGRectGetMaxY(frame) + 5;
        self.imgView.frame = imgFrame;
        //第三方下载图片
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
